{% extends "base_maintain_admin.html" %}

{% block content %}

    <div id="IntegrityMainDiv">

	<table>
		<tbody>
			<tr>
				<th><label for="id_table_name">Table name:</label></th>
				<td><input id="idTableName" type="text" name="table_name" size="3" readonly></td>
                <th rowspan="4"><label for="id_table_name">Missed JRC keys:</label></th>
                <td rowspan="4"><textarea id="idMissedKeys" type="text" name="missed_keys" rows="10" cols="3" readonly style="color: red;"></textarea></td>
			</tr>
			<tr>
				<th><label for="total_records">Total records:</label></th>
				<td><input type="text" name="total_records" id="idTotalRecords" size="3"></td>
			</tr>
			<tr>
				<th><label for="key_missed_total">Key missed total:</label></th>
				<td><input type="text" name="key_missed_total" id="idKeyMissed" size="3" readonly></td>
			</tr>
			<tr>
				<th><label for="key_proceed">Key proceed:</label></th>
				<td><input type="text" name="key_proceed" id="idKeyProceed" size="3" readonly></td>
			</tr>
			<tr>
				<th></th>
				<td><input id="idProceeButton" type="submit" class="default" value="Proceed" onclick="OnJrcIntegrityProceedClicked(); return false;"></td>
			</tr>
        </tbody>
	</table>
</div>

    <script type="text/javascript">

	var gTableName = "{{ table_name }}";
	var gTotalRecords = 0;
	var gKeyMissed = 0;
	var gProceeded = 0;
    var gArMissedKeys = new Array();
    
	var gIsLoaded = true;
	var gIsSent = true;

    window.onload = function () {
        JrcIntegrityInit();
        JrcIntegrityRedraw();
    };

	function JrcIntegrityInit()
	{
		if( gIsLoaded )
		{
			gIsLoaded = false;
			jQuery.ajax({
				url: "/maintain/getcount/{{ table_name }}"
				, success: function(data){ OnJrcIntegrityCounted(data); }
				, error: function(){ alert("Error getting number of records!"); gIsLoaded = true; }
			});
		}
	}

	function OnJrcIntegrityProceedClicked()
	{
		//jQuery("#idProceeButton").attr("disabled", "true");
		JrcGetMissedKeys();
	}

	function JrcGetMissedKeys()
	{
		if( gIsLoaded )
		{
			console.debug("Loading missed keys...");
			gIsLoaded = false;
			jQuery.ajax({
				url: "/maintain/getmissedkeys/{{ table_name }}/" + gProceeded
				, success: function(data){ OnJrcMissedKeysLoaded(data); }
				, error: function(){ alert("Error getting missed keys!"); gIsLoaded = true; }
			});
		}
	}

	function OnJrcMissedKeysLoaded(data)
	{
		gIsLoaded = true;
		console.debug("Got new portion of integrity check...");
		//console.debug("Proceed: " + data["Proceed"]);
		var missed = data["TotalMissed"] ? parseInt(data["TotalMissed"]) : 0;
		var proceed = data["Proceed"] ? parseInt(data["Proceed"]) : 0;
		console.debug("Number of missed kyes: " + missed);
		gProceeded += proceed;
		gKeyMissed += missed;
        
        if( missed > 0 )
        {
            console.debug("Length of array is: " + gArMissedKeys.length);
            for( var i = 0; i < missed; i++ )
            {
                gArMissedKeys[gArMissedKeys.length] = data["Missed"][i];
            }
        }

		JrcIntegrityRedraw();
	}

	function OnJrcIntegrityCounted(data)
	{
		gIsLoaded = true;
		if( data["Total"] )
		{
			gTotalRecords = parseInt(data["Total"]);
		}
		JrcIntegrityRedraw();
	}
    
    
	function JrcIntegrityRedraw()
	{
		jQuery("#idTotalRecords").val(gTotalRecords);
		jQuery("#idKeyMissed").val(gKeyMissed);
		jQuery("#idKeyProceed").val(gProceeded);

		if( gTotalRecords <= gProceeded )
		{
			jQuery("#idProceedButton").attr("disabled", "false");
		}
     
        var s = "";
        if( gArMissedKeys.length > 0 )
        {
            for( var i = 0; i < gArMissedKeys.length; i++ )
            {
                s += gArMissedKeys[i] + "\n";
            }
        }
        jQuery("#idMissedKeys").val(s);
	}

    </script>

{% endblock %}
